package com.k.demo.controller;

import java.io.File;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.Evaluation;
import com.k.demo.service.EvaluationService;

@CrossOrigin
@RestController
@RequestMapping("/evaluation")
public class EvaluationController {
	@Autowired
	private EvaluationService evaluationService;
	@Value("${lc.upload.path}")
	private String uploadPath;
	
	@RequestMapping("/add")
	public Object addEvaluation(@RequestParam("user_id") Integer user_id,@RequestParam("food_id") Integer food_id,@RequestParam("evaluation_content") String evaluation_content,@RequestParam("evaluation_lv") Integer evaluation_lv,@RequestParam MultipartFile[] mpfs, HttpServletRequest request) {
	//用params不用body	
		
		Evaluation evaluation = new Evaluation(user_id,food_id,evaluation_content,evaluation_lv);
		if(mpfs.length>0) {
			String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + "_" + mpfs[0].getOriginalFilename();
			String filePath = uploadPath + "\\" + saveFileName;
			System.out.println(filePath);

			File f = new File(filePath);
			System.out.println(f.exists());
				
			try {
				mpfs[0].transferTo(f);
			} catch (Exception e) {
				e.printStackTrace();
			}
						
			evaluation.setEvaluation_img(saveFileName);
		}
		
		
		int count = evaluationService.addEvaluation(evaluation);
		Map<String,Object> map = new HashMap<String, Object>();
		if (count >0) {
			map.put("Add", true);
		}else {
			map.put("Add", false);
		}
		
		
		return map;
	}
	
	@RequestMapping("/exadd")
	public Object exAddEvaluation(HttpSession session,@RequestParam("food_id") Integer food_id,@RequestParam("evaluation_content") String evaluation_content,@RequestParam("evaluation_lv") Integer evaluation_lv,@RequestParam(value = "file",required = false) MultipartFile file) {
	//用params不用body	
		CustomerUser user = (CustomerUser)session.getAttribute("loginUser");
		Evaluation evaluation = new Evaluation(user.getUser_id(),food_id,evaluation_content,evaluation_lv);
		if(file!=null) {
			String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + "_" + file.getOriginalFilename();
			String filePath = uploadPath + "/" + saveFileName;
			System.out.println(filePath);

			File f = new File(filePath);
			try {
				file.transferTo(f);
			} catch (Exception e) {
				e.printStackTrace();
			}
						
			evaluation.setEvaluation_img(saveFileName);
		}
		
		
		
		int count = evaluationService.addEvaluation(evaluation);
		Map<String,Object> map = new HashMap<String, Object>();
		if (count >0) {
			map.put("Add", true);
		}else {
			map.put("Add", false);
		}
		
		
		return map;
	}
	
	@RequestMapping("/remove")
	public Object removeEvaluation(@RequestBody Evaluation evaluation) {
		int count = evaluationService.deleteEvaluation(evaluation.getEvaluation_id());
		
		Map<String,Object> map = new HashMap<String, Object>();
		if (count >0) {
			map.put("Remove", true);
		}else {
			map.put("Remove", false);
		}
		
		
		return map;
	}
	
	@RequestMapping("/show")
	public Object showEvaluation(@RequestParam int food_id) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("EvaluationList", evaluationService.getEvaluationByFood(food_id));
		
		return map;
	}
	@RequestMapping("/getwithimg")
	public Object getWithImg(@RequestParam int food_id) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("EvaluationList", evaluationService.getWithImg(food_id));
		
		return map;
	}
	
	@RequestMapping("/getbyquality")
	public Object getQuality(@RequestParam int food_id) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("EvaluationList", evaluationService.getWithQuality(food_id));
		
		return map;
	}
	
	@RequestMapping("/count")
	public Object count(@RequestParam int food_id) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("count_num", evaluationService.count(food_id));
		//
		return map;
	}
	@RequestMapping("/getavglv")
	public Object getAvgLv(@RequestParam int food_id) {
		Map<String,Object> map = new HashMap<String, Object>();
		DecimalFormat df = new DecimalFormat("#.#");
		double lv = evaluationService.getAvgEvaluationLv(food_id);
		String lv_1=df.format(lv);
		map.put("avg_lv", Double.parseDouble(lv_1));
		//
		return map;
	}
}
